//
//  BuyCartViewController.m
//  cityStar
//
//  Created by joyban on 15-2-1.
//  Copyright (c) 2015年 tiny. All rights reserved.
//

#import "BuyCartViewController.h"
#import "HTDefine.h"
#import "SideViewController.h"
#import "BuycartTableViewCell.h"
#import "LineView.h"
#import "MLNavigationController.h"
#import "PayCommodityViewController.h"
#import "ReadyPayViewController.h"
#import "popView.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"
#import "UIColor+FlatUI.h"
#import "Buycart.h"
#import "BuycartGood.h"
#import "LoginCityStarViewViewController.h"
#import "BaseNavController.h"
#import "SVPullToRefresh.h"

NSString *const buycartCellID = @"BuycartCellID";

@interface BuyCartViewController ()<CSBackViewDelegate,BuycartTableViewCellDelegate>

@property (nonatomic,strong)popView *pop;
@property (nonatomic,assign) BOOL isDeleteButtonTouch;
@property (nonatomic,assign) BOOL isAllCommiditySelect;
@property (nonatomic,strong) NSMutableArray *requestArray;
@property (nonatomic,strong) NSMutableArray *selectArray;
@property (nonatomic,strong) NSMutableArray *peopleSelectArray;
@property (nonatomic,strong) NSMutableArray *payCommodityArray;
@property (nonatomic,assign) int page;

@end

@implementation BuyCartViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    [self initNavBar];
    [self setUpTableViews];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    self.pop = [[popView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.pop.alpha = 0;
    self.pop.delegate = self;
    [self.view addSubview:self.pop];
    
    self.btnSubmit.clipsToBounds = YES;
    self.btnSubmit.layer.cornerRadius = 4;
    self.selectArray = [NSMutableArray array];
//    self.peopleSelectArray = [[NSMutableArray alloc]init];
}

#pragma mark - UIKeyBoard Notification Methods
- (void)keyboardWillShow:(NSNotification *)notification
{
    [self changeViewFrameWithKeyBoardNotification:notification isShow:YES];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
    [self changeViewFrameWithKeyBoardNotification:notification isShow:NO];
}

- (void)changeViewFrameWithKeyBoardNotification:(NSNotification *)notification isShow:(BOOL)isShow
{
    NSDictionary *userInfo = [notification userInfo];
    //NSValue *keyFrameValue = userInfo[UIKeyboardFrameEndUserInfoKey];
    //CGRect keyboardFrame = [keyFrameValue CGRectValue];
    
    NSNumber *animationDur = userInfo[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *animationCur = userInfo[UIKeyboardAnimationCurveUserInfoKey];
    
    [UIView beginAnimations:@"changeViewFrame" context:nil];
    [UIView setAnimationCurve:[animationCur integerValue]];
    [UIView setAnimationDuration:[animationDur doubleValue]];
    [self.view setFrame:CGRectMake(0, isShow?-182:64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))];
    [UIView commitAnimations];
}

-(void)viewWillAppear:(BOOL)animated
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
        self.navigationController.navigationBar.translucent = NO; //禁止半透明毛玻璃效果
        self.navigationController.navigationBar.tintColor = kWhiteNavTitleColor; //返回按钮颜色
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor]; //bar背景颜色
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}; //标题颜色
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }else{
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]]; //bar背景颜色
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }
    [self webRequest_GetBuyCartWithPage:NO];
}

#pragma mark - setups
-(void)setUpTableViews
{
    self.tableView.backgroundColor = [UIColor colorWithRed:0.946 green:0.942 blue:0.912 alpha:1.000];
    self.tableView.frame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height - 60);
    [self.tableView registerNib:[BuycartTableViewCell nib] forCellReuseIdentifier:buycartCellID];
    
    __weak typeof(self)weakSelf = self;
    [self.tableView addPullToRefreshWithActionHandler:^{
        [weakSelf webRequest_GetBuyCartWithPage:NO];
    }];
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf webRequest_GetBuyCartWithPage:YES];
    }];
    [self.tableView triggerPullToRefresh];
}

- (void)initNavBar{
    self.navigationItem.title = @"购物车";
    
    self.isDeleteButtonTouch = NO;
    UIButton * regBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
    regBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [regBtn setTitleColor:[UIColor colorWithWhite:0.151 alpha:1.000] forState:UIControlStateNormal];
    [regBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [regBtn setTitle:@"完成" forState:UIControlStateSelected];
    regBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [regBtn setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
    [regBtn addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * iHaveNewsBtnItem = [[UIBarButtonItem alloc]initWithCustomView:regBtn];
    self.navigationItem.rightBarButtonItem = iHaveNewsBtnItem;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 30)];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(-30, 0, 90, 30)];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftButton setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"back_touch"] forState:UIControlStateHighlighted];
    
    [view addSubview:leftButton];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.leftBarButtonItem = barButtonItem;
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
//    [[SideViewController shareVc] showLeftView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - webRequest
-(void)webRequest_GetBuyCartWithPage:(BOOL )isMore
{
    NSString *postUrl = [NSString stringWithFormat:@"%@%i",KInterface_GetBuyCart,isMore?self.page:0];
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            if (isMore) {
                [self.requestArray addObjectsFromArray:response[@"value"][@"content"]];
                [self.requestArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                    Buycart *buycart = [Buycart objectFromDictionary:obj];
                    NSMutableArray *array = [NSMutableArray array];
                    for (int i = 0; i<buycart.goods.count; i++) {
                        [array addObject:@"0"];
                    }
                    [self.selectArray addObject:array];
                }];
                self.page++;
            }else{
                self.requestArray = [[NSMutableArray alloc]initWithArray:response[@"value"][@"content"]];
                self.selectArray = [[NSMutableArray alloc]init];
                [self.requestArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
                    Buycart *buycart = [Buycart objectFromDictionary:obj];
                    NSMutableArray *array = [NSMutableArray array];
                    for (int i = 0; i<buycart.goods.count; i++) {
                        [array addObject:@"0"];
                    }
                    [self.selectArray addObject:array];
                }];
            }
            [self.tableView reloadData];
        }else if ([response[@"code"] intValue] == 60007){
            if (isMore) {
                
            }else{
                self.requestArray = [[NSMutableArray alloc]init];
                [self.tableView reloadData];
            }
        }else{
            if ([errMsg isEqualToString:@"请先登入"] == YES) {
                self.page = 1;
                [self presentLoginViewController];
            }
            //            [SVProgressHUD showErrorWithStatus:errMsg];
        }
        if (isMore) {
            [self.tableView.infiniteScrollingView stopAnimating];
        }else{
            [self.tableView.pullToRefreshView stopAnimating];
        }
    }];
}

#pragma mark - IsJumpToLoginViewController
-(void)presentLoginViewController
{
    LoginCityStarViewViewController *loginCityStarVC = [[LoginCityStarViewViewController alloc]init];
    BaseNavController *baseloginNav = [[BaseNavController alloc] initWithRootViewController:loginCityStarVC];
    [self presentViewController:baseloginNav animated:YES completion:^{
    }];
}

#pragma mark - Actions
- (void)searchAction:(UIButton *)btn{
    if (self.isDeleteButtonTouch == YES) {
        btn.selected = NO;
        self.labelMoeny.hidden = NO;
        self.labelFreight.hidden = NO;
        self.isDeleteButtonTouch = NO;
        [self.btnSubmit setTitle:@"结算" forState:UIControlStateNormal];
    }else{
        btn.selected = YES;
        self.labelMoeny.hidden = YES;
        self.labelFreight.hidden = YES;
        self.isDeleteButtonTouch = YES;
        [self.btnSubmit setTitle:@"删除" forState:UIControlStateNormal];
    }
}

-(IBAction)jumpToPayView_Touch:(id)sender
{
    if (self.isDeleteButtonTouch == YES) {
        //        DLog(@"%@",self.peopleSelectArray);
        RIButtonItem *leftButton = [RIButtonItem itemWithLabel:@"确定" action:^{
            [self.payCommodityArray enumerateObjectsUsingBlock:^(BuycartGood *obj, NSUInteger idx, BOOL *stop) {
                NSString *postUrl = [NSString stringWithFormat:@"%@%@",kInterface_cartRemove,obj.uuid];
                [HttpRequestManager deleteMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
                    if (success) {
                        [self.selectArray removeAllObjects];
                        [self webRequest_GetBuyCartWithPage:NO];
                    }else{
                        [SVProgressHUD showErrorWithStatus:errMsg];
                    }
                }];
            }];
        }];
        
        RIButtonItem *rightButton = [RIButtonItem itemWithLabel:@"取消"];
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"是否删除选中商品？" message:nil cancelButtonItem:rightButton otherButtonItems:leftButton, nil];
        [alertView show];
    }else{
        [self getselectCommodity];
        if (self.payCommodityArray.count > 0) {
            ReadyPayViewController *readyPayCommodityVC = [[ReadyPayViewController alloc]initWithCommodityArray:self.payCommodityArray andMoney:self.labelMoeny.text];
            [self.navigationController pushViewController:readyPayCommodityVC animated:YES];
        }else{
            [SVProgressHUD showErrorWithStatus:@"请选择您想要购买的商品"];
        }
    }
}


-(IBAction)selectAll_Touch:(UIButton *)sender
{
    if (sender.selected == YES) {
        self.selectArray = [[NSMutableArray alloc]init];
        [self.requestArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
            Buycart *buycart = [Buycart objectFromDictionary:obj];
            NSMutableArray *array = [NSMutableArray array];
            for (int i = 0; i<buycart.goods.count; i++) {
                [array addObject:@"0"];
            }
            [self.selectArray addObject:array];
        }];
        DLog(@"%@",self.selectArray);
    }else{
        self.selectArray = [[NSMutableArray alloc]init];
        [self.requestArray enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
            Buycart *buycart = [Buycart objectFromDictionary:obj];
            NSMutableArray *array = [NSMutableArray array];
            for (int i = 0; i<buycart.goods.count; i++) {
                [array addObject:@"1"];
            }
            [self.selectArray addObject:array];
        }];
    }
    sender.selected = !sender.selected;
    
    DLog(@"%@",self.selectArray);
    [self getselectCommodity];
    
    [self.tableView reloadData];
}

-(void)senderBackClick:(NSString *)nub
{
    NSString *postUrl = [NSString stringWithFormat:@"%@%@",KInterface_cartUpdate,self.pop.uuid];
    [HttpRequestManager putMethod:postUrl parameters:@{@"number":nub} completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        DLog(@"%@",response);
        if (success) {
            [self webRequest_GetBuyCartWithPage:NO];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

-(void)storeSelect_Touch:(UIButton *)sender
{
    if (sender.selected == YES) {
        [self.selectArray[sender.tag] enumerateObjectsUsingBlock:^(NSArray *obj, NSUInteger idx, BOOL *stop) {
            [self.selectArray[sender.tag] replaceObjectAtIndex:idx withObject:@"0"];
        }];
        
        //        NSArray *arr = @[@"0"];
        //        [self.selectArray replaceObjectAtIndex:sender.tag withObject:arr];
        [self.tableView reloadData];
    }else{
        [self.selectArray[sender.tag] enumerateObjectsUsingBlock:^(NSArray *obj, NSUInteger idx, BOOL *stop) {
            [self.selectArray[sender.tag] replaceObjectAtIndex:idx withObject:@"1"];
        }];
        [self.tableView reloadData];
    }
    sender.selected = !sender.selected;
    [self getselectCommodity];
}

-(void)getselectCommodity
{
    self.payCommodityArray = [[NSMutableArray alloc]init];
    [self.requestArray enumerateObjectsUsingBlock:^(NSDictionary *objArr, NSUInteger idx, BOOL *stop) {
        NSArray *arr = self.selectArray[idx];
        //        NSArray *goodArr = [[NSArray alloc]initWithArray:objArr.goods];
        [arr enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL *stop) {
            if ([obj intValue] == 1) {
                DLog(@"%@",objArr);
                BuycartGood *goodcart = [BuycartGood objectFromDictionary:objArr[@"goods"][idx]];
                [self.payCommodityArray addObject:goodcart];
            }
        }];
    }];
    __block float strAllPrice = 0;
    [self.payCommodityArray enumerateObjectsUsingBlock:^(BuycartGood *obj, NSUInteger idx, BOOL *stop) {
        strAllPrice = [obj.price floatValue]*[obj.number intValue]+strAllPrice;
    }];
    self.labelMoeny.text = [NSString stringWithFormat:@"￥ %.2f",strAllPrice];
    
    DLog(@"%@",self.payCommodityArray);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return self.requestArray.count+1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section < self.requestArray.count) {
        return 60;
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section < self.requestArray.count) {
        return 36;
    }else{
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(40, 20, 60, 30)];
    view.backgroundColor = [UIColor whiteColor];
    
    UIView *downView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 20)];
    downView.backgroundColor = [UIColor colorWithRed:0.946 green:0.942 blue:0.912 alpha:1.000];
    [view addSubview:downView];
    
    
    
    UIButton *btnselete = [[UIButton alloc]initWithFrame:CGRectMake(8, 24, 30, 30)];
    [btnselete setBackgroundImage:[UIImage imageNamed:@"check_n"] forState:UIControlStateNormal];
    [btnselete setBackgroundImage:[UIImage imageNamed:@"check_y"] forState:UIControlStateSelected];
    NSArray *array = self.selectArray[section];
    if ([array[0] boolValue]) {
        btnselete.selected = YES;
    }
    btnselete.tag = section;
    [btnselete addTarget:self action:@selector(storeSelect_Touch:) forControlEvents:UIControlEventTouchUpInside];
    //    if (self.isAllCommiditySelect == YES) {
    //        btnselete.selected = YES;
    //    }else{
    //        btnselete.selected = NO;
    //    }
    [view addSubview:btnselete];
    UILabel *labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(45, 25, 180, 30)];
    
    Buycart *buycart = [Buycart objectFromDictionary:self.requestArray[section]];
    labelTitle.text = buycart.supplier;
    
    labelTitle.font = [UIFont systemFontOfSize:14];
    labelTitle.textColor = [UIColor darkGrayColor];
    [view addSubview:labelTitle];
    UIImageView *lineViewdown = [[UIImageView alloc]initWithFrame:CGRectMake(15, 59, 320, 1)];
    lineViewdown.backgroundColor = [UIColor colorWithWhite:0.925 alpha:1.000];
    [view addSubview:lineViewdown];
    
    UIImageView *lineViewtop = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20, 320, 1)];
    lineViewtop.backgroundColor = [UIColor colorWithWhite:0.925 alpha:1.000];
    [view addSubview:lineViewtop];
    if (section < self.requestArray.count) {
        return view;
    }else{
        return nil;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    Buycart *buycart = [Buycart objectFromDictionary:self.requestArray[section]];
    __block int number = 0;
    [buycart.goods enumerateObjectsUsingBlock:^(BuycartGood *obj, NSUInteger idx, BOOL *stop) {
        number = [obj.number intValue] + number;
    }];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(40, 20, 45, 70)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(30, 3, 200, 30)];
    labelTitle.text = [NSString stringWithFormat:@"共计%i件商品       合计：",number];
    labelTitle.textAlignment = NSTextAlignmentRight;
    labelTitle.font = [UIFont systemFontOfSize:12];
    labelTitle.textColor = [UIColor darkGrayColor];
    [view addSubview:labelTitle];
    
    UILabel *labelMoney = [[UILabel alloc]initWithFrame:CGRectMake(205, 3, 100, 30)];
    __block float strPrice = 0;
    [buycart.goods enumerateObjectsUsingBlock:^(BuycartGood *obj, NSUInteger idx, BOOL *stop) {
        strPrice = [obj.price floatValue]*[obj.number intValue]+strPrice;
    }];
    labelMoney.text = [NSString stringWithFormat:@"￥ %.2f",strPrice];
    labelMoney.textAlignment = NSTextAlignmentRight;
    labelMoney.textColor = [UIColor colorFromHexCode:@"333333"];
    labelMoney.font = [UIFont systemFontOfSize:17];
    labelMoney.textColor = [UIColor blackColor];
    [view addSubview:labelMoney];
    
    //    UIImageView *lineViewtops = [[UIImageView alloc]initWithFrame:CGRectMake(15, 2, 320, 1)];
    //    lineViewtops.backgroundColor = [UIColor colorWithWhite:0.925 alpha:1.000];
    //    [view addSubview:lineViewtops];
    
    UIView *downView = [[UIView alloc]initWithFrame:CGRectMake(0, 40, 320, 10)];
    downView.backgroundColor = [UIColor colorWithRed:0.946 green:0.942 blue:0.912 alpha:1.000];
    [view addSubview:downView];
    
    UIImageView *lineViewinside = [[UIImageView alloc]initWithFrame:CGRectMake(0, 35, 320, 1)];
    lineViewinside.backgroundColor = [UIColor colorWithWhite:0.925 alpha:1.000];
    [view addSubview:lineViewinside];
    
    if (section < self.requestArray.count) {
        return view;
    }else{
        return nil;
    }
}

#pragma mark - buycartTableViewCellDelegate
-(void)commodityChangeNumberWithIndex:(NSIndexPath *)index
{
    Buycart *buycart = [Buycart objectFromDictionary:self.requestArray[index.section]];
    BuycartGood *goodCart = buycart.goods[index.row];
    self.pop.namelabel.text = [NSString stringWithFormat:@"%@",goodCart.goods_name];
    self.pop.numLabel.text = [NSString stringWithFormat:@"%@",goodCart.number];
    self.pop.pricelabel.text = [NSString stringWithFormat:@"￥%.2f",[goodCart.price floatValue]*[goodCart.number floatValue]];
    self.pop.number = [NSString stringWithFormat:@"%@",goodCart.number];
    [self.pop.imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://api.icszx.com/image/c/id/%@/w/170/h/130/m/1",goodCart.images]] placeholderImage:[UIImage imageNamed:@"商户列表_img"]];
    self.pop.uuid = [NSString stringWithFormat:@"%@",goodCart.uuid];
    self.pop.price = [goodCart.price floatValue];
    //    [self.pop.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://api.icszx.com/image/c/id/%@/w/640/h/300/m/1",goodCart.images] placeholderImage:[UIImage imageNamed:@"商户列表_img"]];
    self.pop.alpha = 1;
    [UIView animateWithDuration:0.3f animations:^{
        [self.pop.cellView setFrame:CGRectMake(0, self.view.frame.size.height - 142, self.view.frame.size.width, 142)];
    }];
}

-(void)buycartTableViewCellTouchSeleceButtonWithIndex:(NSIndexPath *)index andisslelect:(BOOL)select
{
    DLog(@"%li,%li",(long)index.section,(long)index.row);
    if (select) {
        
        [self.selectArray[index.section] replaceObjectAtIndex:index.row withObject:@"1"];
        
        
        DLog(@"%@",self.selectArray);
        [self.tableView reloadData];
    }else{
        [self.selectArray[index.section] replaceObjectAtIndex:index.row withObject:@"0"];
        
        DLog(@"%@",self.selectArray);
        [self.tableView reloadData];
    }
    [self getselectCommodity];
    
}

//去掉UItableview headerview黏性
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //    DLog(@"%f",scrollView.contentOffset.y);
    if (scrollView == self.tableView)
    {
        CGFloat sectionHeaderHeight = 60;
        if (scrollView.contentOffset.y<= sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}
#pragma mark - UITableViewDelegate and UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section < self.requestArray.count) {
        BuycartTableViewCell *buycartTableViewCell = [tableView dequeueReusableCellWithIdentifier:buycartCellID];
        buycartTableViewCell.selectionStyle = UITableViewCellSelectionStyleGray;
        Buycart *buyCart = [Buycart objectFromDictionary:self.requestArray[indexPath.section]];
        BuycartGood *goodCart = buyCart.goods[indexPath.row];
        if ([self.selectArray[indexPath.section][indexPath.row] boolValue]) {
            [buycartTableViewCell configWithselect:YES andGoodCart:goodCart andindex:indexPath];
        }else{
            [buycartTableViewCell configWithselect:NO andGoodCart:goodCart andindex:indexPath];
        }
        buycartTableViewCell.delegate = self;
        //    [self.groupBuyTableViewCell configWithMessage:message];
        return buycartTableViewCell;
    }else{
        static NSString *CellIdentifier = @"AddFrameTableViewCell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor colorWithRed:0.945 green:0.941 blue:0.910 alpha:1.000];
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section < self.requestArray.count) {
        return [BuycartTableViewCell heightOfTableCell];
    }else{
        return 95;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section < self.requestArray.count) {
        Buycart *buyCart = [Buycart objectFromDictionary:self.requestArray[section]];
        return buyCart.goods.count;
    }else{
        return 1;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView reloadData];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
